/*
 * @Author: wuyang
 * @Description:
 * @Date: 2022-12-29 14:58:46
 * @LastEditors: wuyang
 * @LastEditTime: 2023-01-13 10:55:01
 * @FilePath: \vue-ts-vite\src\router\index.ts
 */
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { close, start } from "@/utils/nprogress";
import LoginRouter from "./modules/login";
import ScreenRouter from "./modules/screen";
import StudentRouter from "./modules/student";
import ManageRouter from './modules/manage';
import type { App } from 'vue'
export const publicRoutes: Array<RouteRecordRaw> = [
  ...LoginRouter,
  ...ScreenRouter,
  ...StudentRouter,
  ...ManageRouter,
  {
    path: "/",
    name: "MainIndex",
    redirect:'/login'
  }
,
];



const router = createRouter({
  history: createWebHashHistory(),
  routes: publicRoutes,
});
/**
 * 初始化路由表
 */
export function resetRouter() {
  router.getRoutes().forEach(route => {
    const { name } = route
    if (name) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

// 路由前置后卫
router.beforeEach((to, from) => {
  start();
  // 路由守卫逻辑

  // 通过 return turn;
  // 不通过 return false;
  return true;
});
// 路由后置后卫
router.afterEach(() => {
  close();
});

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router;
